﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Linq;
using System.Data.Linq.Mapping;


namespace TwoWheels.Account
{
    public partial class Register : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
 
            RegisterUser.ContinueDestinationPageUrl = Request.QueryString["ReturnUrl"];  
        }

        protected void RegisterUser_CreatedUser(object sender, EventArgs e)  //wywolane po utworzeniu uzytkownika - tu dodac odniesienie do strony której chce
        {
            FormsAuthentication.SetAuthCookie(RegisterUser.UserName, false /* createPersistentCookie */);
           
         

            string continueUrl = RegisterUser.ContinueDestinationPageUrl;
            if (String.IsNullOrEmpty(continueUrl))
            {
                continueUrl = "~/";
            }

            const string cnStr = @"Data Source=DUO-P8600;Initial Catalog=TwoWheels;Integrated Security=True";
            TwoWheelsDatabase tdb = new TwoWheelsDatabase(cnStr);
            
            TextBox name = RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("Email") as TextBox;
            TextBox surname = RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("UserSurname") as TextBox;
            TextBox eMail = RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("UserName") as TextBox;
            Session["EMAIL"] = String.Copy(eMail.Text);

            TextBox HashCode = RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("Password") as TextBox;

            TextBox nation = RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("NationTextBox") as TextBox;
            TextBox city = RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("CityTextBox") as TextBox;
            TextBox address = RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("AddressTextBox") as TextBox;
            TextBox postCode = RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("PostCodeTextBox") as TextBox;
            TextBox phoneNumber = RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("PhoneTextBox") as TextBox;

            CheckBox company = RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("CompanyCheckBox") as CheckBox;

            TextBox companyName = RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("CompanyNameTextBox") as TextBox;
            TextBox NIP = RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("NIPTextBox") as TextBox;

            Klienci klient = new Klienci { Imie = name.Text, Nazwisko = surname.Text, eMail = eMail.Text, HashCode = HashCode.Text,
                                           Adres = address.Text,
                                           Miasto = city.Text,
                                           Panstwo = nation.Text,
                                           Telefon = phoneNumber.Text,
                                           KodPocztowy = postCode.Text,
                                           NazwaFirmy = companyName.Text.Equals("") ? null : companyName.Text,
                                           NIP = NIP.Text.Equals("") ? null : NIP.Text
            };

            tdb.clients.InsertOnSubmit(klient);
            tdb.SubmitChanges();

         //   Response.Redirect("~/Account/RegisterDetails.aspx"); // bez przekierowania wizard robi jeszcze krok complete
          // Response.Redirect(continueUrl);
            //przekierowac tak aby zrobic pobranie dodatkowych informacji - niech nie bedzie to completeuserwizardstep tylko osobna strona. Jak zawrzeć ją w tej przestrzeni ?
        }

        protected void onCheckChanged(object sender, EventArgs e)
        {
            CheckBox company = RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("CompanyCheckBox") as CheckBox;
            TextBox companyTextBox = RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("CompanyNameTextBox") as TextBox;
            TextBox NIPTextBox = RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("NIPTextBox") as TextBox;

            companyTextBox.Enabled = true;
            companyTextBox.BackColor = System.Drawing.Color.White;

            NIPTextBox.Enabled = true;
            NIPTextBox.BackColor = System.Drawing.Color.White;

        }

        protected void ContinueButton_Click(object sender, EventArgs e)
        {

            const string cnStr = @"Data Source=DUO-P8600;Initial Catalog=TwoWheels;Integrated Security=True";
            TwoWheelsDatabase tdb = new TwoWheelsDatabase(cnStr);
            String data = Session["EMAIL"] as String;

            int ID = (from c in tdb.clients where c.eMail.Equals(data) select c.KlientID).ToList().First();
            
            Session["UserShoppingCartInfo"] = new Account.UserShoppingCart(ID);
            Menu menu = this.Master.FindControl("NavigationMenu") as Menu;
            menu.Items.Add(new MenuItem("Cart", "", "", "~/Basket.aspx")); // przeniesc to do kontruktora koszyka
            Response.Redirect("~/Default.aspx");
        }

    }
}
